package com.eliansun.crawler.pageparse.jiangsu;

import com.eliansun.util.WebDriverPath;
import lombok.extern.slf4j.Slf4j;
import net.dongliu.requests.Requests;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ZW
 * @version 1.0
 * @date 2020/4/27 11:37
 */

@Slf4j
public class Suning implements PageProcessor {

    private static final String URL = "https://yao.suning.com/?safp=d488778a.homepage1.99345513004.51&safpn=10001";//主页

    private int count = 0;

    @Override
    public void process(Page page) {
        String url = page.getUrl().toString();//网址
        if (url.equals(URL)) {
            //首页进入爬取第一次进入次页的链接
            List<String> list1 = page.getHtml().xpath("//html/body/div[5]/div[1]/div/ul/li[4]/p/span/a/@href").all();//中西药品
            setUrl(page,list1);
            /*//list1.size()控制首页爬取第一次次页的链接数量
            for (int i = 0; i < list1.size(); i++) {
                String curl = list1.get(i);
                Request request = new Request();
                request.setUrl(curl);
                page.addTargetRequest(request);
            }*/
        } else if (ifBurl(url)) {
            try {
                //处理第一次链接得到多有分页链接
                String str = page.getHtml().xpath("//*[@id=\"bottom_pager\"]/div/a[5]/@pagenum").get();// 进次页后的总页数
                // String link = page.getHtml().xpath("//*[@id=\"bottom_pager\"]/div/a[5]/@href").get(); // 最后一页链接
                if (str == null || str.isEmpty()) {
                    //进入次页所取得的分页数较少，爬取总页数失败所做的修补
                    List<String> all = page.getHtml().xpath("//*[@id='bottom_pager']//div/a//@href").all();
                    str = (all.size() - 1) + "";
                }
                int pageNum = Integer.parseInt(str);
                String bu = url.replaceAll("0\\.html", "");
                //控制生成的页数:pageNum
                for (int i = 1; i < pageNum; i++) {
                    StringBuffer sb = new StringBuffer(bu);
                    String burl = sb.append(i).append(".html").toString();
                    Request request = new Request();
                    request.setUrl(burl);
                    page.addTargetRequest(request);
                }
            } catch (Exception e) {
                log.error("爬取次页面所有翻页有错误！");
            }
        }
        if (ifCurl(url)) {
            //详情页所需链接的爬取
            try {
                String html = getHtml(url);
                Elements elementsGoods = Jsoup.parse(html).select("div[id=product-list]").select("ul").select("li");
                for (Element elementsGood : elementsGoods) {
                    String attr = elementsGood.select("div").select("div").select("div").eq(3).select("a").attr("href");
                    String curl;
                    if (!attr.contains("https:")) {
                        curl = "https:" + attr;
                        Request request = new Request();
                        request.setUrl(curl);
                        page.addTargetRequest(request);
                    } else {
                        curl = attr;
                        String id = elementsGood.attr("id");
                        String[] split = id.split("-");
                        Map<String, Object> map = new HashMap<>();
                        map.put("num2", split[0]);
                        map.put("num1", split[1]);
                        Request request = new Request();
                        request.setUrl(curl);
                        request.setExtras(map);
                        page.addTargetRequest(request);
                    }
                    //爬取的苏宁商品链接有些事以https:开头的有些则没有，需要处理
                }
            } catch (Exception e) {
                log.error("详情页链接爬取有误！");
            }
        }
        if (ifDurl(url)) {

            String productCompany = "";//生产企业
            String medicalDeviceNo = "";//医疗器械注册证编号
            String productName = "";//药品名称
            String spec = "";//规格
            String price = "";//价格
            String brand = "";//品牌
            String executiveStandard = "";//执行标准
            String nationality = "";//国产进口
            String store = "";//卖家
            String version = "";//型号
            Map<String, Object> resultMap = new HashMap<>();
            try {
                List<String> names = page.getHtml().xpath("//*[@id=\"itemParameter\"]/tbody/tr/td[1]/div/span/text()").all();
                List<String> values = page.getHtml().xpath("//*[@id=\"itemParameter\"]/tbody/tr/td[2]/text()").all();
                if (names.size() == 0) {
                    return;//排除商品不存在的情况
                }
                if (names.size() == values.size()) {
                    for (int i = 0; i < names.size(); i++) {
                        String name = names.get(i);
                        if (name.contains("生产企业")) {
                            productCompany = values.get(i);
                        }
                        if (name.contains("文号")) {
                            medicalDeviceNo = values.get(i);
                        }
                        if (name.contains("药品名称")) {
                            productName = values.get(i);
                        }
                        if (name.contains("规格")) {
                            spec = values.get(i);
                        }
                        if (name.contains("品牌")) {
                            brand = page.getHtml().xpath("//*[@id=\"itemParameter\"]/tbody/tr/td[2]/a/text()").get();
                        }
                        if (name.contains("使用剂量")) {
                            executiveStandard = values.get(i);
                        }
                        store = page.getHtml().xpath("//*[@id=\"shopName\"]/a/text()").get();

                    }
                }
                String num1 = "";//url中倒数第二串数字
                String num2 = "";//url中倒数第一串数字
                if (url.contains("cmdCode")) {
                    num1 = (String) page.getRequest().getExtra("num1");
                    num2 = (String) page.getRequest().getExtra("num2");
                } else {
                    String[] split = url.split("/");
                    List<String> list = new ArrayList<>();
                    for (String s : split) {
                        String[] split1 = s.split("\\.");
                        list.addAll(Arrays.asList(split1));
                    }
                    list = list.stream().filter(e -> (!e.equals(""))).collect(Collectors.toList());
                    num1 = list.get(list.size() - 2);
                    num2 = list.get(list.size() - 3);
                }
                String getUrl = "https://pas.suning.com/nspcsale_0_";//价格请求前缀
                StringBuffer sb = new StringBuffer(getUrl);
                String Durl = sb.append(getSize(num1)).append(num1).append("_").append(getSize(num1)).append(num1).append("_").append(num2).append("_100_025.html").toString();
                String readToText = Requests.get(Durl).send().readToText();
                String[] split1 = readToText.split(",");
                for (String s : split1) {
                    if (s.contains("promotionPrice")) {
                        price = getprice(s);
                        break;
                    }
                }

            } catch (Exception e) {
                log.error("页面信息爬取有误!");
            }
            resultMap.put("spec", spec);//
            resultMap.put("medicalDeviceNo", medicalDeviceNo);//
            resultMap.put("productCompany", productCompany);//
            resultMap.put("price", price);
            resultMap.put("brand", brand);
            resultMap.put("productName", productName);
            resultMap.put("executiveStandard", executiveStandard);
            resultMap.put("nationality", nationality);
            resultMap.put("store", store);
            resultMap.put("url", url);
            resultMap.put("version", version);
            count++;
            log.info("这是第" + count + "条数据！");
            page.putField("result", resultMap);
        }
    }

    private String getHtml(String url) {
        WebDriver webDriver = WebDriverPath.getWebDriver(url);
        try {
            ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 2000)");//滚动条
            Thread.sleep(3000);
            ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 2000)");//防止懒加
            Thread.sleep(800);
            ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 4000)");//滚动条
            Thread.sleep(3000);
            ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 4000)");//防止懒加
            Thread.sleep(800);
            ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 6000)");//滚动条
            Thread.sleep(3000);
            ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 6000)");//防止懒加
            Thread.sleep(800);
            ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 8000)");//滚动条
            Thread.sleep(3000);
            ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 8000)");//防止懒加
            Thread.sleep(800);
            ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 10000)");//滚动条
            Thread.sleep(3000);
            ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 10000)");//防止懒加
            Thread.sleep(800);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String html = webDriver.getPageSource();
        webDriver.close();
        return html;
    }

    private void setUrl(Page page, List<String> all) {
        //all.size()控制爬取详情页的链接数量
        for (int i = 0; i < all.size(); i++) {
            String curl = all.get(i);
            Request request = new Request();
            request.setUrl(curl);
            page.addTargetRequest(request);
        }
    }

    private String getprice(String str) {
        String[] split = str.split(":");
        return split[1];
    }

    private String getSize(String nums) {
        int nu = 18 - nums.length();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < nu; i++) {
            sb.append("0");
        }
        return sb.toString();
    }

    private boolean ifBurl(String url) {
        if (url.contains("10.html") || url.contains("20.html") || url.contains("30.html") || url.contains("40.html") || url.contains("50.html")) {
            return false;
        }
        return url.contains("0.html");
    }

    private boolean ifCurl(String url) {
        return url.contains("/0-");
    }

    private boolean ifDurl(String url) {
        return url.split("/").length == 5 || url.contains("cmdCode");


    }


    private Site site = Site.me()
            .setCycleRetryTimes(5)
            .setRetryTimes(5)
            .setSleepTime(1000)
            .setTimeOut(3 * 60 * 1000)
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
            .addHeader("Accept-Language", "zzh-CN,zh;q=0.8")
            .addHeader("Accept-Encoding", "zh-CN,zh;q=0.8");


    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new Suning())
                .addUrl(URL)
                //设置多个线程同时抓取数据
                .thread(10)
                .run();
    }

      /*//这个爬取详情页的链接只爬取了30条，页面中鼠标向下滑动再调API加载另外的,每页预计120条，
                List<String> all = page.getHtml().xpath("//*[@id='product-list']/ul//li//div[@class='item-bg']//div[@class='product-box ']//div[@class='res-info']//div[@class='title-selling-point']//a//@href").all();
                //all.size()控制每页爬取的详情链接
                setUrl(page, all);*/

    //https://search.suning.com/%E8%A1%80%E5%8E%8B%E8%AE%A1/ 血压计链接
    //https://list.suning.com/0-500808-0.html       健脾益肾 链接      第一次进入链接
    //https://list.suning.com/0-500808-1.html

    //List<String> list2 = page.getHtml().xpath("//html/body/div[5]/div[1]/div/ul/li[6]/p/span/a/@href").all();//医疗器械
    //list1.addAll(list2);*///现在只爬取药品的信息链接，器械的暂不需要


    //https://list.suning.com/0-500808-13.html   14   健脾益肾尾页链接
    //https://search.suning.com/%E8%A1%80%E5%8E%8B%E8%AE%A1/&iy=0&isNoResult=0&cp=6  7 血压计链接第一页
    //https://search.suning.com/%E8%A1%80%E7%B3%96/&iy=0&isNoResult=0&cp=18

    //https://product.suning.com/0070501012/10984472855.html
    // https://product.suning.com/0070233336/11221433107.html
    //

    //https://pas.suning.com/nspcsale_0_000000010799823981_000000010799823981_0000000000_100_025.html?
    // 详情页

    //page.getHtml().xpath("//html/body/div[5]/div[1]/div/ul/li[4]/p/span/a/@href").all();//中西药品
    //page.getHtml().xpath("//html/body/div[5]/div[1]/div/ul/li[6]/p/span/a/@href").all();//医疗器械


    //https://list.suning.com/0-500808-0.html  第一次进次页

    //*[@id="0000000000-10583459954"]

    //*[@id="product-list"]

    //page.getHtml().xpath("//*[@id='product-list']//ul//li//div//div//div[2]//div[2]//a//@href").all();

    //String str = page.getHtml().xpath("//*[@id=\"bottom_pager\"]/div/a[5]/@pagenum").get(); 进次页后的总页数
    //String link = page.getHtml().xpath("//*[@id=\"bottom_pager\"]/div/a[5]/@href").get();  最后一页链接

    //*[@id="bottom_pager"]/div/a[last()-1]

    //*[@id="itemParameter"]/tbody/tr[4]/td[1]/div/span   药品名称
    //*[@id="itemParameter"]/tbody/tr[4]/td[2]

    //*[@id="itemParameter"]/tbody/tr[3]/td[1]/div/span   类别
    //*[@id="itemParameter"]/tbody/tr[3]/td[2]
}
